package com.zhongyewx.teachercert.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotifyClickActivity;
import com.zhongyewx.teachercert.R;
import com.zhongyewx.teachercert.ZYApplication;
import com.zhongyewx.teachercert.api.ZYARouterApi;
import org.android.agoo.common.AgooConstants;

import butterknife.BindView;
import butterknife.ButterKnife;
import pub.devrel.easypermissions.EasyPermissions;

public class ZYWelcomeActivity extends UmengNotifyClickActivity {

    @BindView(R.id.tv_welcome)
    TextView tvWelcome;
    @BindView(R.id.layout_welcome)
    ConstraintLayout layoutWelcome;
    private AlphaAnimation alphaAnimation;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_welcome_layout);
        ButterKnife.bind(this);
        ZYApplication.getInstance().addActivity(this);
        PushAgent.getInstance(this).onAppStart();//统计
        initView();
    }


    public void initView() {
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {//防止每次点击home键再进入重启的问题
            finish();
            return;
        }
        alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                aRouterTurnLogin(ZYARouterApi.AROUTER_MAIN_ACTY);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        tvWelcome.startAnimation(alphaAnimation);

//        SnackbarUtil.showSnackbar(layoutWelcome, "哈哈哈哈");
//        checkPermission();
    }

    @Override
    public void onMessage(Intent intent) {
        super.onMessage(intent);
        if (intent == null) {
            return;
        }
        super.onMessage(intent);  //此方法必须调用，否则无法统计打开数
        String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        ZYApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }
    /**
     * 6.0以下版本(系统自动申请) 不会弹框
     * 有些厂商修改了6.0系统申请机制，他们修改成系统自动申请权限了
     */
    private void checkPermission() {
        String[] perms = new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        EasyPermissions.requestPermissions(this, "KotlinMvp应用需要以下权限，请允许", 0, perms);
    }

    private void aRouterTurnLogin(String path){

        ARouter.getInstance().build(path).navigation(this, new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {
                finish();
            }

            @Override
            public void onInterrupt(final Postcard postcard) {
                ARouter.getInstance()
                        .build(ZYARouterApi.AROUTER_LOGIN_ACTY)
                        //拦截跳转的时候，后续登录成功后，继续跳转，需要这个url
                        .greenChannel()
//                            .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation(ZYWelcomeActivity.this);
                finish();
            }
        });
    }
//    @Override
//    public void request() {
//
//    }
//
//    @Override
//    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
//        if (requestCode == 0) {
//            if (!ListUtils.isEmpty(perms)) {
//                if (perms.contains(Manifest.permission.READ_PHONE_STATE)
//                        && perms.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//                    if (alphaAnimation != null) {
//                        tvWelcome.startAnimation(alphaAnimation);
//                    }
//                }
//            }
//        }
//    }
}
